package com.lv.hechuang.widget;

import android.annotation.SuppressLint;

import com.google.android.material.bottomnavigation.BottomNavigationItemView;
import com.google.android.material.bottomnavigation.BottomNavigationMenuView;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.lang.reflect.Field;

/**
 * 文件名:    BottomNavigationViewHelper
 * 创建者:    Saber  2018/3/12 13:25
 * 描述: 用来处理BottomNavigationView item 超过三个时  去除动画效果
 * <p>
 * Activity中代码如下
 * BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
 * BottomNavigationViewHelper.disableShiftMode(navigation);
 */

public class BottomNavigationViewHelper {


    @SuppressLint("RestrictedApi")
    public static void disableShiftMode(BottomNavigationView view) {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
        try {
            Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
            shiftingMode.setAccessible(true);
            shiftingMode.setBoolean(menuView, false);
            shiftingMode.setAccessible(false);
            for (int i = 0; i < menuView.getChildCount(); i++) {
                BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
                item.setChecked(item.getItemData().isChecked());
            }
        } catch (NoSuchFieldException e) {
        } catch (IllegalAccessException e) {
        }
    }


}
